<template>
  <!-- 添加或修改生产订单和产品合计关联对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <!--<el-form ref="form" :model="form" :rules="rules" label-width="120px">-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="projectNo">
        <el-input v-model="queryParams.projectNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="xinghao">
        <el-input v-model="queryParams.xinghao" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品描述" prop="miaoshu">
        <el-input v-model="queryParams.miaoshu" placeholder="请输入产品描述" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                     v-hasPermi="['develop:FactoryProduceSaleAll:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                     @click="handleUpdate" v-hasPermi="['develop:FactoryProduceSaleAll:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-popconfirm title="确认删除选择的数据项？" @confirm="handleDelete({})">
            <el-button slot="reference" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                       v-hasPermi="['develop:FactoryProduceSaleAll:remove']">删除</el-button>
          </el-popconfirm>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini"
                     @click="handleExport" v-hasPermi="['develop:FactoryProduceSaleAll:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>-->

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
      :max-height="tableMaxHeight" show-summary :summary-method="getSummaries">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="生产订单id" align="center" prop="produceId"/>
        <el-table-column label="订单产品id" align="center" prop="saleId"/>
        <el-table-column label="采购订单id" align="center" prop="caigouId"/>-->
      <el-table-column sortable label="订单编号" align="center" prop="projectNo" show-overflow-tooltip />
      <el-table-column sortable label="工程名称" align="center" prop="gongchengName"></el-table-column>
      <el-table-column sortable align="center" label="产品编码" prop="code">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
            <span @click="showDetailGongxu(scope.row)" style="color: #00afff;cursor:pointer;">
              {{ scope.row.code }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="name" />
      <el-table-column sortable align="center" label="产品型号" prop="xinghao">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
            <span @click="shengchanDoGoodsSale(scope.row)" style="color: #00afff;cursor:pointer;">
              {{ scope.row.xinghao }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="产品数量" align="center" prop="count" />
      <el-table-column label="生产完成" align="center" prop="finishdoCount" />
      <el-table-column label="入库数量" align="center" prop="rukuCount" />
      <el-table-column sortable label="产品类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.prod_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column sortable label="产品状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.prodsale_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column sortable label="工序状态" align="center" prop="issheding">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_status" :value="scope.row.issheding" />
        </template>
      </el-table-column>
      <el-table-column sortable label="拆解状态" align="center" prop="ischaijie">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_chaijie_gongxu" :value="scope.row.ischaijie" />
        </template>
      </el-table-column>
      <el-table-column label="需求日期" align="center" prop="saleDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品描述" align="center" prop="miaoshu" show-overflow-tooltip />
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="fenpeirenwu(scope.row)" v-if="showDisplay != 1">
            分配任务
          </el-button>
          <el-button @click="chaijie(scope.row)" icon="el-icon-setting" size="mini" type="text" v-if="showDisplay != 1">
            材料拆解
          </el-button>
          <el-button @click="yongliaoMingxi(scope.row)" size="mini" type="text" icon="el-icon-document">
            用料明细
          </el-button>
          <!--<el-button @click="qualityTest(scope.row)" icon="el-icon-view" size="mini" type="text">
            质检情况
          </el-button>-->
          <el-button @click="shengchanDoGoodsSale(scope.row)" icon="el-icon-setting" size="mini" type="text">
            生产过程
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" v-if="showDisplay != 1" @click="handleUpdate(scope.row)"
            v-hasPermi="['develop:FactoryProduceSaleAll:edit']">修改</el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" v-if="showDisplay != 1" @confirm="handleDelete(scope.row)" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
              v-hasPermi="['develop:FactoryProduceSaleAll:remove']">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!--</el-form>-->
    <factoryProduceSaleAll-form ref="form" @ok="getList"></factoryProduceSaleAll-form>
    <chajie-form @ok="getList" ref="chaijieForm"></chajie-form>
    <yongliaoMingxi-form-list ref="yongliaoMingxiForm" @ok="getList"></yongliaoMingxi-form-list>
    <!--<factory-test-quality-goods ref="qualityTestingGoods" @ok="getList"> </factory-test-quality-goods>-->
    <shengchanDoGoodsSale-form @ok="getList" ref="shengchanDoGoodsSaleForm"></shengchanDoGoodsSale-form>
    <gongxuProd-form @ok="getList" ref="gongxuForm"></gongxuProd-form>
    <shengchanDoGongxu-form @ok="getList" ref="shengchanDoGongxuForm"></shengchanDoGongxu-form>

  </el-dialog>
</template>

<script>
import { deleteAction, getById, getAction, accAdd } from '@/api/manage';
import moment from 'moment';
import FactoryProduceSaleAllForm from '@/views/develop/factory/factoryProduceSaleAll/module/FactoryProduceSaleAllForm'
import ChajieForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChajieForm";
import YongliaoMingxiFormList from '@/views/develop/goodsSaleMaterial/module/YongliaoMingxiFormList'
//import FactoryTestQualityGoods from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityGoods.vue";
import ShengchanDoGoodsSaleForm from '@/views/develop/goodsSale/module/ShengchanDoGoodsSaleForm.vue';
import GongxuProdForm from '@/views/develop/zizhi/GongxuProdForm.vue';
import ShengchanDoGongxuForm from '@/views/develop/goodsSale/module/ShengchanDoGongxuForm.vue';

export default {
  name: "GoodsDoAllForm",
  dicts: ['prodsale_status', 'pro_chaijie_gongxu', 'prod_type', 'task_status'],
  components: {
    FactoryProduceSaleAllForm,
    ChajieForm,
    YongliaoMingxiFormList,
    //FactoryTestQualityGoods,
    ShengchanDoGoodsSaleForm,
    GongxuProdForm,
    ShengchanDoGongxuForm,
  },
  data() {
    return {
      showDisplay:'',
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产订单和产品合计关联表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        produceId: null,
        projectNo: null,
        saleId: null,
        caigouId: null,
        count: null,
        code: null,
        name: null,
        xinghao: null,
        miaoshu: null
      },
      tableMaxHeight: window.innerHeight - 350,
      url: {
        list: '/factoryProduceSaleAll/factoryProduceSaleAll/list',
        remove: '/factoryProduceSaleAll/factoryProduceSaleAll/removeNoTask/',
      },
    };
  },
  created() {

  },
  methods: {
    showDetailGongxu(row) {
      this.$refs.shengchanDoGongxuForm.show(row);
    },
    getSummaries(param) {
      // console.log(param);
      const { columns, data } = param;
      const sums = [];

      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "count"
        ) {

          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
    fenpeirenwu(row) {
      this.$refs.gongxuForm.gongxuShow(row);
    },
    chaijie(row) {
      this.$refs.chaijieForm.chaijieGoods(row);
    },
    yongliaoMingxi(row) {
      this.$refs.yongliaoMingxiForm.show(row);
    },
    qualityTest(row) {
      this.$refs.qualityTestingGoods.showByRukuDetail(row);
    },
    shengchanDoGoodsSale(row) {
      this.$refs.shengchanDoGoodsSaleForm.show(row);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      deleteAction(this.url.remove, id).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    show(row,showDisplay) {
      this.showDisplay = showDisplay;
      this.queryParams.produceId = row.id;
      this.open = true;
      this.title = "生产产品信息";
      this.getList();
    },
    /** 查询生产订单和产品合计关联列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  }
};
</script>
